import re

from django import forms
from django.core.exceptions import ValidationError
from multiselectfield import MultiSelectFormField

from sales import models


def mobile_validate(value):
    mobile_re = re.compile(r'^(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    if not mobile_re.match(value):
        raise ValidationError('手机号码格式错误')


class RegisterForm(forms.Form):
    username = forms.CharField(
        max_length=16,
        min_length=1,
        label='用户名',
        widget=forms.widgets.TextInput(attrs={'class': 'username', 'autocomplete': 'off', 'placeholder': '请输入用户名'}),
        error_messages={
            'required': '用户名不能为空',
            'max_length': '用户名不能大于16位',
            'min_length': '用户名不能小于6位',
        }
    )
    password = forms.CharField(
        max_length=16,
        min_length=6,
        label='密码',
        widget=forms.widgets.PasswordInput(
            attrs={'class': 'password', 'onpaste': "return false", 'oncontextmenu': "return false",
                   'placeholder': "输入密码", }
        ),
        error_messages={
            'required': '密码不能为空',
            'max_length': '密码不能大于16位',
            'min_length': '密码不能小于6位',
        }
    )
    r_password = forms.CharField(
        max_length=16,
        min_length=6,
        label='确认密码',
        widget=forms.widgets.PasswordInput(
            attrs={'class': 'password', 'onpaste': "return false", 'oncontextmenu': "return false",
                   'placeholder': "请再次输入密码", }),
        error_messages={
            'required': '确认密码不能为空',
        }
    )
    email = forms.EmailField(
        label='邮箱',
        # validators=[],
        error_messages={
            'required': '邮箱不能为空',
            'invalid': '邮箱格式不对'
        },
        widget=forms.widgets.TextInput(
            attrs={'type': 'email', 'class': 'email', 'placeholder': '输入邮箱地址', 'oncontextmenu': 'return false'})
    )
    telephone = forms.CharField(
        label='电话号码',
        validators=[mobile_validate, ],
        error_messages={
            'required': '电话号码不能为空',
        },
        widget=forms.widgets.TextInput(
            attrs={'class': 'phone_number', 'placeholder': '输入手机号码', 'autocomplete': 'off', 'id': 'number'}),
    )

    def clean(self):
        values = self.cleaned_data
        r_password = values.get('r_password')
        password = values.get('password')
        if password == r_password:
            return values
        else:
            self.add_error('r_password', '两次输入的密码不一致')


class CustomerForm(forms.ModelForm):
    class Meta:
        model = models.Customer
        fields = '__all__'

        error_messages = {
            'qq': {'required': '不能为空'},
            'course': {'required': '不能为空'},
        }

    def __init__(self, *args, **kwargs):
        from django.forms.fields import DateField
        super().__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
            if isinstance(field, DateField):
                field.widget.attrs.update({'type': 'date'})
            if not isinstance(field, MultiSelectFormField):
                field.widget.attrs.update({'class': 'form-control'})
